package cn.edu.lingnan.service;

import cn.edu.lingnan.mapper.CourseMapper;
import cn.edu.lingnan.mapper.ScoreMapper;
import cn.edu.lingnan.mapper.StudentMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class StudentServiceMysqlImpl implements StudentService{
    @Override
    public void deleteStudent(String sid) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentMapper studentMapper = applicationContext.getBean("studentMapper", StudentMapper.class);
        ScoreMapper scoreMapper = applicationContext.getBean("scoreMapper", ScoreMapper.class);
        CourseMapper courseMapper = applicationContext.getBean("courseMapper", CourseMapper.class);
//        删除学生
//        首先要查找学生选了哪些课加入到list1
        List<String> list1=scoreMapper.selectCidFromScoreBySid(sid);
//                找出哪些课可以删除加到list2
        List<String> list2=new ArrayList<>();
        for(String cid:list1){
            if(scoreMapper.selectNumberFromScoreByCid(cid)==1){
                list2.add(cid);
            }
        }
        scoreMapper.deleteScoreBySid(sid);
        studentMapper.deleteStudentBySid(sid);
        for(String cid:list2){
            courseMapper.deleteCourseByCid(cid);
        }
    }
}
